#if !SILVERLIGHT
using System.Security.Principal;
using System.Collections.Generic;

namespace SBPweb.Common.Authorization
{
	/// <summary>
	/// Class of IdentityHandler
	/// </summary>
	public sealed class IdentityHandler
	{
		#region Private members

		private static string KEY_SESSION_IDENTITY = "Session_Identity_Key";

		#endregion

		#region Constructor logic

		private IdentityHandler()
		{
		}

		#endregion

		#region Public property accessors

		/// <summary>
		/// Currently used identity
		/// </summary>
		public static WindowsIdentity CurrentIdentity
		{
			get
			{
				WindowsIdentity identity = Session.SessionHandler.Get<WindowsIdentity>(KEY_SESSION_IDENTITY);
				if (identity == null)
				{
					identity = WindowsIdentity.GetCurrent();
					Session.SessionHandler.Put<WindowsIdentity>(KEY_SESSION_IDENTITY, identity);
				}
				return identity;
			}
			set
			{
				Session.SessionHandler.Put<WindowsIdentity>(KEY_SESSION_IDENTITY, value);
			}
		}

		/// <summary>
		/// Currently used principal
		/// </summary>
		public static WindowsPrincipal CurrentPrincipal
		{
			get
			{
				return new WindowsPrincipal(CurrentIdentity);
			}
		}

		/// <summary>
		/// Currently used context
		/// </summary>
		public static IDictionary<string, object> CurrentContext
		{
			get
			{
				WindowsIdentity wId = CurrentIdentity;
				lock (wId)
				{
					if (wId != null && wId.User != null)
					{
						Dictionary<string, object> dict = new Dictionary<string, object>();
						List<string> keywords = new List<string>();
						keywords.Add("identity: " + wId.Name);
						dict.Add("Keywords", keywords);
						return dict;
					}
					else
					{
						return new Dictionary<string, object>();
					}
				}
			}
		}

		#endregion
	}
}
#endif